package com.crx.springbootinit.controller.user;


import cn.hutool.core.bean.BeanUtil;
import com.crx.springbootinit.annotation.LogApiAnno;
import com.crx.springbootinit.common.BaseResponse;
import com.crx.springbootinit.common.ResultUtils;
import com.crx.springbootinit.common.WebSocketServer;
import com.crx.springbootinit.model.dto.chat.*;
import com.crx.springbootinit.model.dto.page.PageVO;
import com.crx.springbootinit.model.entity.Chat;
import com.crx.springbootinit.model.vo.chat.ChatUserVO;
import com.crx.springbootinit.model.vo.chat.ChatVO;
import com.crx.springbootinit.model.vo.chat.Chatted;
import com.crx.springbootinit.service.IChatService;
import com.crx.springbootinit.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 聊天消息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-21
 */
@Api(tags = "聊天消息模块接口")
@RestController
@RequestMapping("user/chat")
@RequiredArgsConstructor
public class ChatController {

    private final IChatService chatService;
    @PostMapping("/send")
    @ApiOperation("发送消息")
    @LogApiAnno(title = "聊天模块", businessType = "POST")
    public BaseResponse<Void> send(@RequestBody ChatSendDTO chatSendDTO) throws IOException {
        Chat chat = BeanUtil.copyProperties(chatSendDTO, Chat.class);
        Long userId = ThreadLocalUtil.get("userId");
        chat.setUserId(userId);
        Boolean isRead = WebSocketServer.sendInfo(chat);
        //如果接收方在线则为true
        chat.setIsRead(isRead);
        chatService.save(chat);
        return ResultUtils.success();
    }

    @ApiOperation("获取聊天记录")
    @GetMapping("/history")
    @LogApiAnno(title = "聊天模块", businessType = "GET")
    public BaseResponse<ChatUserVO> getHistory(ChatHistoryDTO chatHistoryDTO){
        ChatUserVO chatUserVO = chatService.getHistory(chatHistoryDTO);
        return ResultUtils.success(chatUserVO);
    }

    @ApiOperation("获取未读消息的数量")
    @GetMapping("/unreadCount")
    @LogApiAnno(title = "聊天模块", businessType = "GET")
    public BaseResponse<Long> getUnreadCount(ChatUnreadCountDTO chatUnreadCountDTO){
        return ResultUtils.success(chatService.lambdaQuery()
                .eq(Chat::getTargetId,chatUnreadCountDTO.getUserId())
                .eq(Chat::getUserId,chatUnreadCountDTO.getTargetId())
                .eq(Chat::getIsRead,false)
                .count());
    }

    @ApiOperation("标记消息为已读")
    @GetMapping("/markAsRead")
    @LogApiAnno(title = "聊天模块", businessType = "GET")
    public BaseResponse<Void> markAsRead(ChatMarkReadDTO chatMarkReadDTO){
        chatService.markAsRead(chatMarkReadDTO);
        return ResultUtils.success();
    }

    @ApiOperation("获取在线用户列表")
    @GetMapping("online")
    @LogApiAnno(title = "聊天模块", businessType = "GET")
    public BaseResponse<List<Long>> getOnlineUser(){
        List<Long> list=new ArrayList<>(WebSocketServer.getOnlineUser().keySet());
        return ResultUtils.success(list);
    }

    @ApiOperation("获取消息通知列表")
    @GetMapping("getChatted")
    @LogApiAnno(title = "聊天模块", businessType = "GET")
    public BaseResponse<PageVO<Chatted>> getChattedList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        PageVO<Chatted> chattedList = chatService.getChattedList(pageNum,pageSize);
        return ResultUtils.success(chattedList);
    }
}
